/**
 ### 资源管理表 v5_resource

 name : string 资源名称
 owner : string 资源归属人
 type : string 资源类型
 appId: string 资源来源

 数据内容,根据type分别使用以下字段
 url : string 如果类型是网址
 html: stirng 如果类型是html
 json: object 如果类型是json
 */
const Parse = require('parse/node');
const ResourceObj = Parse.Object.extend("v5_resource");
const ParseHelper = require('../helpers/parse');


async function createJSON(appId, data, extra, username) {
    const resRecord = new ResourceObj();
    const name = data.substring(0, 5) + "...";
    resRecord.set("name", name);// 资源名称
    resRecord.set("owner", username);//资源归属人
    resRecord.set("type", "json");//资源类型
    resRecord.set("appId", appId);//资源来源
    resRecord.set("json", data);//数据内容
    resRecord.set("extra", extra);
    resRecord.set("isDeleted", false);
    return resRecord.save(null, {useMasterKey: true});
}

async function createCSV(appId, data, extra, username) {
    const resRecord = new ResourceObj();
    const name = data.substring(0, 5) + "...";
    resRecord.set("name", name);// 资源名称
    resRecord.set("owner", username);//资源归属人
    resRecord.set("type", "csv");//资源类型
    resRecord.set("appId", appId);//资源来源
    resRecord.set("csv", data);//数据内容
    resRecord.set("extra", extra);
    resRecord.set("isDeleted", false);
    return resRecord.save(null, {useMasterKey: true});
}

function createFile(appId, data, extra, uploadResult, username) {
    let resourceType = "url/file";
    const resourceName = uploadResult && uploadResult.originalname || "";
    if (resourceName.endsWith(".pdf")) {
        resourceType = "url/file-pdf";
    } else if (resourceName.endsWith(".jpg")) {
        resourceType = "url/file-image";
    } else if (resourceName.endsWith(".png")) {
        resourceType = "url/file-image";
    } else if (resourceName.endsWith(".jfif")) {
        resourceType = "url/file-image";
    }

    const resRecord = new ResourceObj();
    resRecord.set("name", resourceName);// 资源名称
    resRecord.set("owner", username);//资源归属人
    resRecord.set("type", resourceType);//资源类型
    resRecord.set("appId", appId);//资源来源
    resRecord.set("url", uploadResult && uploadResult.url);//数据内容
    resRecord.set("extra", extra);
    resRecord.set("isDeleted", false);
    return resRecord.save(null, {useMasterKey: true});
}


function findResourceByUsernameWithType(username, type) {
    const query = new Parse.Query(ResourceObj);
    query.equalTo('owner', username);
    query.equalTo('type', type);
    query.notEqualTo("isDeleted", true);
    query.select('url');
    query.descending("updatedAt");
    query.limit(100);
    return query.find({useMasterKey: true}).then(ParseHelper.toJSONList);
}

function findCSVResourceByUsernameWithType(username) {
    const query = new Parse.Query(ResourceObj);
    query.equalTo('owner', username);
    query.equalTo('type', 'csv');
    query.notEqualTo("isDeleted", true);
    query.limit(99999);
    return query.find({useMasterKey: true}).then(ParseHelper.toJSONList);
}


function add(name,owner,type,data,root,level,resourceType){
    const resRecord = new ResourceObj();
    resRecord.set("name", name);// 资源名称
    resRecord.set("owner", owner);//资源归属人
    if(type==='file'||type==='url'){
        resRecord.set("type", resourceType);//资源类型
        resRecord.set("url", data);//数据内容
    }
    if(type==='html'){
        resRecord.set("type", type);//资源类型
        resRecord.set("html", data);//数据内容
    }
    resRecord.set("root", root);
    resRecord.set("level", level);
    return resRecord.save(null, {useMasterKey: true});
}

// 更新资源
async function update(key,value,id){
    const resource= ResourceObj.createWithoutData(id);
    resource.set(key,value);
    await resource.save(null,{ useMasterKey: true });
    return ParseHelper.toJSON(resource);
}

module.exports = {
    createFile,
    createJSON,
    createCSV,
    findResourceByUsernameWithType,
    findCSVResourceByUsernameWithType,
    add,
    update
};